﻿using Engine;
using Game;
using Game.NetWork;
using Game.NetWork.Packages;
using Game.Server;
using Game.Server.Event;
using Game.Server.PlayerEvent;
using GameEntitySystem;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Mail;
using System.Text;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Threading.Tasks;
using System.Xml;


namespace 帮人写的插件
{
    public class 单独说话插件 : AbstractProcessCmd
    {
        public override string Cmd => "systemsay";

        public override string Introduce => "/systemsay     ---系统单独说话插件";

        public override int AuthLevel => 100;

        public ComponentPlayer 说话人对象;

        public override DisplayType Display => DisplayType.All;

        private void SendHelpMessage()
        {
            string text = "/systemsay all   -----给全部玩家发送系统消息\n/systemsay user [玩家名] [消息]     ----给指定玩家发送系统消息\n";
            base.SendMessage("查看所有say命令=====>>>", text);
        }

        public override void ProcessCmd()
        {
            if (m_messageDatas.Length <= 1)
            {
                SendHelpMessage();
                return;
            }
            if (m_isTerminal == true)
            {
                base.SendMessage("系统单独说话插件", "请在游戏客户端中输入命令！");
                return;
            }
            else
            {
                ComponentPlayer fsa = this.m_client.PlayerData.ComponentPlayer;
                SubsystemPlayers 全部玩家 = GameManager.Project.FindSubsystem<SubsystemPlayers>();
                if (m_messageDatas[1] == ("all".ToLower())) //给全部玩家发送系统消息 ToLower代表忽略大小写
                {
                    if (m_messageDatas[2] != null)
                    {
                        foreach (PlayerData playerData in 全部玩家.PlayersData)
                        {
                            说话人对象 = playerData.ComponentPlayer;
                            string message = string.Join(" ", m_messageDatas.Skip(2));
                            DisplaySmallMessageNet(说话人对象, message);
                        }
                    }
                    else if (m_messageDatas[2] == null)
                    {
                        SendHelpMessage();
                        DisplaySmallMessageNet(fsa, "请输入要发送的消息！");
                    }
                }
                if (m_messageDatas[1] == ("user".ToLower()))
                {
                    bool 状态 = false;
                    if (m_messageDatas[2] != null && m_messageDatas[3] != null)
                    {
                        foreach (PlayerData playerData in 全部玩家.PlayersData)
                        {
                            if (m_messageDatas[2].ToString() == playerData.Name.ToString())
                            {
                                说话人对象 = playerData.ComponentPlayer;
                                状态 = true;
                            }
                        }
                        if (状态 == true)
                        {
                            string message = string.Join(" ", m_messageDatas.Skip(3));
                            DisplaySmallMessageNet(说话人对象, message);
                        }
                        if (状态 == false)
                        {
                            DisplaySmallMessageNet(fsa, "玩家不存在！");
                        }
                    }
                }
                if (m_messageDatas[1] == ("area".ToLower()))
                {
                    List<PlayerData> playerData = new List<PlayerData>();
                    float x = float.Parse(m_messageDatas[2]);
                    float y = float.Parse(m_messageDatas[3]);
                    float z = float.Parse(m_messageDatas[4]);

                    float x1 = float.Parse(m_messageDatas[5]);
                    float y1 = float.Parse(m_messageDatas[6]);
                    float z1 = float.Parse(m_messageDatas[7]);

                    foreach (PlayerData playerData1 in 全部玩家.PlayersData)
                    {
                        if (IsPlayerInArea(new Vector3(x, y, z), new Vector3(x1, y1, z1), playerData1.ComponentPlayer))
                        {
                            playerData.Add(playerData1);
                        }
                    }
                    if(playerData.Count == 0 || playerData == null)
                    {
                        DisplaySmallMessageNet(fsa, "玩家不在区域内！");
                    }
                    else
                    {
                        for (int i = 0; i < playerData.Count; i++)
                        {
                            DisplaySmallMessageNet(fsa, $"{playerData[i].Name}玩家在区域内！");
                        }
                    }
                }
            }


        }
        public void DisplaySmallMessageNet(ComponentPlayer componentPlayer, string text)  // 小消息方法
        {
            var messagePackget = new MessagePackage(null, text, 0, null);
            messagePackget.To = componentPlayer.PlayerData.Client;
            CommonLib.Net.QueuePackage(messagePackget);
        }

        public bool IsPlayerInArea(Vector3 position1, Vector3 position2, ComponentPlayer componentPlayer)
        {
            Vector3 playerPos = componentPlayer.ComponentBody.Position;

            // 计算区域的min和max坐标
            float minX = Math.Min(position1.X, position2.X);
            float maxX = Math.Max(position1.X, position2.X);
            float minY = Math.Min(position1.Y, position2.Y);
            float maxY = Math.Max(position1.Y, position2.Y);
            float minZ = Math.Min(position1.Z, position2.Z);
            float maxZ = Math.Max(position1.Z, position2.Z);

            // 检查玩家是否在区域内
            return playerPos.X >= minX && playerPos.X <= maxX &&
                   playerPos.Y >= minY && playerPos.Y <= maxY &&
                   playerPos.Z >= minZ && playerPos.Z <= maxZ;
        }


    }
}
